package jp.kentaro.plugin.socket.client.views;

import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

public class PacketLabelProvider extends LabelProvider
	implements ITableLabelProvider, ITableColorProvider {

	private Display display;
	
	private Color[] pallet;
	private Image texture;
	private ImageRegistry imageRegistry;
	private ColorRegistry colorRegistry;
	private int chipWidth = 16;
	
	PacketLabelProvider(Display display, Image sourceImage){
		this.display = display;
		texture = new Image(this.display, 16, 16);
		GC gc = new GC(texture);
		gc.drawImage(sourceImage, 0, 0, 32, 32, 0, 0, 16, 16);
		gc.dispose();
		
		// Create background color of Cell
		pallet = new Color[16];
		pallet[0x00] = new Color(display, 0x00, 0x00, 0x00);
		pallet[0x01] = new Color(display, 0x00, 0x00, 0xFF);
		pallet[0x02] = new Color(display, 0x00, 0xFF, 0x00);
		pallet[0x03] = new Color(display, 0x00, 0xFF, 0xFF);
		pallet[0x04] = new Color(display, 0xFF, 0x00, 0x00);
		pallet[0x05] = new Color(display, 0xFF, 0x00, 0xFF);
		pallet[0x06] = new Color(display, 0xFF, 0xFF, 0x00);
		pallet[0x07] = new Color(display, 0xFF, 0xFF, 0xFF);
		pallet[0x08] = new Color(display, 0x80, 0x80, 0x80);
		pallet[0x09] = new Color(display, 0x00, 0x00, 0x80);
		pallet[0x0A] = new Color(display, 0x00, 0x80, 0x00);
		pallet[0x0B] = new Color(display, 0x00, 0x80, 0x80);
		pallet[0x0C] = new Color(display, 0x80, 0x00, 0x00);
		pallet[0x0D] = new Color(display, 0x80, 0x00, 0x80);
		pallet[0x0E] = new Color(display, 0x80, 0x80, 0x00);
		pallet[0x0F] = new Color(display, 0xC0, 0xC0, 0xC0);
		
		colorRegistry = new ColorRegistry();
		for (Color color : pallet) {
			colorRegistry.put(color.toString(), color.getRGB());
		}
		
		imageRegistry = new ImageRegistry();
	}
	
	private Image getChipImage(PacketDatum datum) {
		  Image result = imageRegistry.get(Integer.toString(datum.id));
		  // [(a) この位置に「なければ作る」のコードを記述
		  if(result == null){
			  result = new Image(display, chipWidth, chipWidth);
			  // [(b) この位置に「なければ作る」のコードを記述
			    GC gc = new GC(result);
			    int bg = datum.width % pallet.length;
			    gc.setBackground(pallet[bg]);
			    gc.fillRectangle(result.getBounds());
			    gc.dispose();
		  }
		  return result;
	}
	
	public Image getColumnImage(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		if (columnIndex == 5) {
            return texture;
        }
		return null;
	}

	public String getColumnText(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		PacketDatum datum = (PacketDatum)element;
		
		switch(columnIndex) {
		case 0:
			return Integer.toString(datum.id);
		case 1:
			return Integer.toString(datum.width);
		case 2:
			return Integer.toString(datum.height);
		case 3:
			return Integer.toString(datum.offset_x);
		case 4:
			return Integer.toString(datum.offset_y);		
		}
		return null;
	}

	public Color getBackground(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		if (element instanceof PacketDatum) {
			PacketDatum datum = (PacketDatum) element;
			if(columnIndex == 1) {
				// put the color along to "Width"
				int index = datum.width % pallet.length;
				return pallet[index];
			}
		}
		
		if(columnIndex == 1){
			
		}
		return null;
	}

	public Color getForeground(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		return null;
	}

}
